package com.wk.code.wkserver.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wk.code.wkserver.constant.NumberConstant;
import com.wk.code.wkserver.entity.Feedback;
import com.wk.code.wkserver.entity.TechType;
import com.wk.code.wkserver.entity.User;
import com.wk.code.wkserver.exception.BizException;
import com.wk.code.wkserver.mapper.FeedbackMapper;
import com.wk.code.wkserver.mapper.TechTypeMapper;
import com.wk.code.wkserver.request.admin.AdminFeedbackRequest;
import com.wk.code.wkserver.request.admin.TechTypeRequest;
import com.wk.code.wkserver.request.app.FeedbackRequest;
import com.wk.code.wkserver.response.admin.FeedbackResponse;
import com.wk.code.wkserver.response.admin.TechTypeResponse;
import com.wk.code.wkserver.response.base.PageInfo;
import com.wk.code.wkserver.service.IFeedbackService;
import com.wk.code.wkserver.service.ITechTypeService;
import com.wk.code.wkserver.service.IUserService;
import com.wk.code.wkserver.utils.DateUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 技术类别表 服务实现类
 * </p>
 *
 * @author wq
 * @since 2021-08-15
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class FeedbackServiceImpl extends ServiceImpl<FeedbackMapper, Feedback> implements IFeedbackService {


    @Resource
    private IUserService userService;

    @Override
    public void save(Integer userId, FeedbackRequest request) {
        if (StringUtils.isEmpty(request.getContent())) {
            throw new BizException("请填写反馈内容");
        }
        Feedback selectOne = baseMapper.selectOne(new LambdaQueryWrapper<Feedback>().eq(Feedback::getUserId, userId).
                eq(Feedback::getContent, request.getContent()));
        if (Objects.nonNull(selectOne)) {
            throw new BizException("相同内容不能重复反馈");
        }
        Feedback feedback = new Feedback();
        feedback.setContent(request.getContent());
        if (!CollectionUtils.isEmpty(request.getImages())) {
            if (request.getImages().size() > NumberConstant.SIX) {
                throw new BizException("最多上传6张图片哦");
            }
            feedback.setImages(JSONObject.toJSONString(request.getImages()));
        }
        feedback.setUserId(userId);
        save(feedback);
    }

    @Override
    public PageInfo<FeedbackResponse> page(AdminFeedbackRequest request) {
        IPage<Feedback> page = new Page<>(request.getPageNumber(), request.getPageSize());
        LambdaQueryWrapper<Feedback> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(request.getContent())) {
            wrapper.like(Feedback::getContent, request.getContent());
        }
        wrapper.orderByDesc(Feedback::getCreateTime);
        List<Feedback> records = baseMapper.selectPage(page, wrapper).getRecords();
        if (!CollectionUtils.isEmpty(records)) {
            List<FeedbackResponse> list = records.stream().map(x -> {
                FeedbackResponse response = new FeedbackResponse();
                User user = userService.selectById(x.getUserId());
                if (Objects.nonNull(user)) {
                    response.setUsername(user.getNickname());
                    response.setAvatar(user.getAvatar());
                    response.setRegisterType(user.getRegisterType());
                }
                response.setContent(x.getContent());
                if (StringUtils.isNotEmpty(x.getImages())) {
                    response.setImages(JSONObject.parseArray(x.getImages()).getString(0));
                }
                response.setCreateTime(DateUtils.localDateTime2Date(x.getCreateTime()));
                response.setUserId(x.getUserId());
                return response;
            }).collect(Collectors.toList());
            return new PageInfo<>(page.getTotal(), list);
        }
        return null;
    }
}
